package org.aeon.heroicsagatextedition.game;

import org.aeon.heroicsagatextedition.Input.InputFactory;
import org.aeon.heroicsagatextedition.constants.CommonlyConstants;
import org.aeon.heroicsagatextedition.game.Backup.GameBackup;
import org.aeon.heroicsagatextedition.role.factory.HeroFactory;
import org.aeon.heroicsagatextedition.role.factory.MonsterFactory;
import org.aeon.heroicsagatextedition.role.factory.RoleUnitFactory;
import org.aeon.heroicsagatextedition.role.product.RoleUnit;
import org.aeon.heroicsagatextedition.round.factory.RoundFactory;
import org.aeon.heroicsagatextedition.round.product.Round;

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Iterator;

/**
 * @projectName: HeroicSagaTextEdition
 * @package: org.aeon.heroicsagatextedition.game
 * @className: Game
 * @author: MissingAeon
 * @date: 2023/12/22 13:21
 * @description: 游戏的主类，负责整体游戏逻辑和流程控制。
 */
public class Game {
    private static final Game game = new Game();
    private static Deque<RoleUnit> roles;
    private Round firstRound = null;

    public static Game getGame() {
        return game;
    }

    /**
     * 新游戏
     */
    public void newGame() {
        wipeData();
        init();
        gameProgress("新游戏开始！");
    }

    public void wipeData() {
        GameBackup.cleanUp();
    }

    /**
     * 继续游戏
     */
    public void continueGame() {
        if (GameBackup.isExistenceMemento()) {
            roles = GameBackup.getRoles();
            initializeRound();
            firstRound.setRecordCount(GameBackup.getRecordCount() - 1);
            gameProgress("游戏继续！");
        } else {
            System.out.println("不存在备份，请开始新游戏！");
        }
    }

    /**
     * 退出
     */
    public void quit() {
        System.out.println("谢谢游玩！再见！");
        System.exit(0);
    }

    public void summary() {
        // TODO 游戏结束可以总结伤害
    }

    /**
     * 初始化：
     * 角色创建
     */
    private void init() {
        initializeRole();
        initializeRound();
    }

    /**
     * 回合初始化
     */
    private void initializeRound() {
        RoundFactory roundFactory = new RoundFactory();
        Iterator<RoleUnit> iterator = roles.iterator();
        // 获取每一次回合的值
        Round round = null;
        // 这里创建了回合链
        while (iterator.hasNext()) {
            if (round != null) {
                // 先调用上一个round的setSuccessor方法设置下一个继承者，然后将round设置为继承者
                round.setSuccessor(round = roundFactory.createRound(iterator.next()));
            } else {
                // 首次无需设置上一次的继承者
                round = roundFactory.createRound(iterator.next());
                firstRound = round;
                firstRound.setRoles(roles);
            }
        }
    }

    /**
     * 角色初始化
     */
    private void initializeRole() {
        RoleUnitFactory heroFactory = new HeroFactory();
        RoleUnitFactory monsterFactory = new MonsterFactory();
        roles = new ArrayDeque<>();
        roles.add(createHero(heroFactory));
        for (int i = 0; i < 3; i++) {
            roles.add(monsterFactory.createRoleUnit(
                    CommonlyConstants.MONSTER_NAMES[
                            ((int) (Math.random() * 20)) % CommonlyConstants.MONSTER_NAMES.length]));
        }
    }

    /**
     * 创建英雄
     *
     * @param heroFactory
     * @return
     */
    private RoleUnit createHero(RoleUnitFactory heroFactory) {
        System.out.print("请输入您的英雄名称：");
        String name =InputFactory.create().scanString();
        return heroFactory.createRoleUnit(name);
    }


    /**
     * 游戏进程
     *
     * @param news
     */
    private void gameProgress(String news) {
        System.out.println(news);
        GameSettlement.setState(true);
        InputFactory.create().pauseBeforeInput();
        while (GameSettlement.isState()) {
            firstRound.showModifyRecordCount(firstRound.getRecordCount() + 1);
            firstRound.matchingRound();
        }
    }
}
